<template>
	<view class="content">
		<z-paging ref="pagingRef" v-model="dataList" @query="queryList" :fixed="false" :auto-show-back-to-top="true"
			:auto-clean-list-when-reload="true">
			<view class="item" v-for="(item, index) in dataList" :key="item.userId" @click="itemClick(item)">
				<!-- 人员信息结构 -->
				<view class="uni-flex uni-align-center top">
					<view class="photo-box">
						<image :src="item.avatar " class="photo" />
					</view>
					<view class="uni-flex-column" style="flex: 1">
						<view class="uni-flex uni-align-center uni-justify-between box1">
							<view class="uni-flex">
								<text class="title">{{item.name||'--'}}</text>
								<view class="real-name">
									<text class="text1">“{{item.serviceTitle}}”</text>
								</view>
							</view>
							<view class="distance-box">
								<image :src="`${$ImgBaseUrl}detail_location.png`" mode="widthFix" class="location" />
								<text
									class="text1">{{item.distance===null||item.distance===''?'--':item.distance}}km</text>
							</view>
						</view>
						<view class="box2">
							<text class="text1">{{item.workDuration||'--'}}</text>
							<text class="text1">经验</text>
							<text class='text2'>|</text>
							<text class="text1">{{item.regAddress||'--'}}</text>
						</view>
					</view>
				</view>
				<view class="uni-flex box3"  v-if="item?.services && item.services.length > 0">
					<view class="con">
						<template>
							<view v-for="(s, i) in item.services" :key="i" class="text2">{{ s }}</view>
						</template>
					</view>
					
				</view>
				<view class="footer">
					<!-- 收藏图标 -->
					<image :src="`${$ImgBaseUrl}detail_collect.png`" mode="widthFix" class='colletc'
						v-if="!item.collect" @click.stop="handleCollect(item, index)" />
					<image :src="`${$ImgBaseUrl}lookforsb-collect-selected.png`" class='colletc' mode="widthFix"
						v-if="item.collect" @click.stop="handleCollect(item, index)" />
				</view>
			</view>
			<template #empty>
				<view class="empty-state">
					<text class="empty-text">暂无数据</text>
				</view>
			</template>
		</z-paging>
	</view>
</template>

<script setup>
	import {
		ref,
		watch,
		nextTick
	} from "vue";
	import {
		userCollectApi
	} from "@/api/personalFavorites.js";
	import {
		PeopleDetailCollect,
	} from '@/api/findPeople.js'

	const props = defineProps({
			tabIndex: {
				type: Number,
				default: 0
			},
			currentIndex: {
				type: Number,
				default: 0
			},
		
			browseType: {
				type: String,
				default: ''
			},
		
		});


	const pagingRef = ref(null);
	const dataList = ref([]);

	// 监听父组件currentIndex变化
	watch(
		() => props.currentIndex,
		(newVal) => {
			if (newVal === props.tabIndex) {
				pagingRef.value?.reload();
			}
		}, {
			immediate: true
		}
	);


	const queryList = async (pageNum, pageSize) => {
		try {
			const params = {
				pageNum: pageNum,
				pageSize: pageSize,
				browseType: props.browseType
			};

			const res = await userCollectApi(params);
			if (res && res.code === 200) {
				const result = res.rows || [];
				pagingRef.value.complete(result);
			} else {
				pagingRef.value.complete(false);
			}
		} catch (error) {
			console.error('接口调用失败:', error);
			pagingRef.value.complete(false);
		}
	};

	// 收藏/取消收藏处理函数
	const handleCollect = async (item, index) => {
		try {
			uni.showLoading({
				title: '处理中...',
				mask: true
			});

			const params = {
				collectId: item.userId,
				collectType: 'USER',
				isCollect: !item.collect
			};

			const res = await PeopleDetailCollect(params);

			if (res && res.code === 200) {
				// 判断是否为取消收藏操作
				if (!item.collect) {
					// 收藏操作：仅更新状态
					item.collect = true;
					uni.showToast({ title: '收藏成功', icon: 'success', duration: 2000 });
				} else {
					// 取消收藏操作：从列表中移除
					// 方法1：直接删除数组元素（适用于客户端状态管理）
					dataList.value.splice(index, 1);
					
					// 方法2：如果需要严格与服务器同步，可重新加载列表
					// pagingRef.value.reload();
					
					uni.showToast({ title: '取消收藏成功', icon: 'success', duration: 2000 });
				}
			} else {
				uni.showToast({
					title: res?.msg || '操作失败',
					icon: 'none',
					duration: 2000
				});
			}
		} catch (error) {
			console.error('收藏接口调用失败:', error);
			uni.showToast({
				title: '操作失败，请重试',
				icon: 'none',
				duration: 2000
			});
		} 
	};

	const itemClick = (item) => {
		uni.navigateTo({
			url: `/pages/tabBars/findPeople/peopleDetail?jobId=${item.userId}`
		});
	};

	const reload = () => {
		pagingRef.value?.reload();
	};

	defineExpose({
		reload
	});
</script>

<style lang="scss" scoped>
	.content {
		height: calc(100vh - 250rpx);
	}

	.item {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx 20rpx;
		margin: 26rpx 24rpx 0;

		.top {
			padding-bottom: 18rpx;

			.photo-box {
				position: relative;

				.photo {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					margin-right: 24rpx;
				}

				.online {
					position: absolute;
					bottom: 0;
					left: 10rpx;
					color: white;
					background: #6bc98d;
					border-radius: 17rpx;
					padding: 5rpx 0;
					font-weight: 500;
					width: 80rpx;
					text-align: center;
					font-size: 22rpx;
				}
			}

			.box1 {
				margin-bottom: 9rpx;

				.title {
					font-size: 32rpx;
					font-weight: 500;
					color: #333333;
					margin-right: 8rpx;
				}

				.real-name {
					font-weight: 400;
					font-size: 24rpx;
					color: #FF6A47;
					display: flex;
					align-items: center;
				}

				.sign-contract {
					background: #e5f0ff;
					border-radius: 17px;
					border: 1px solid #2f80ed;
					margin-left: 8rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					width: 116rpx;
					height: 34rpx;

					image {
						width: 20rpx;
						height: 20rpx;
						margin-right: 8rpx;
					}

					.text1 {
						font-weight: 500;
						font-size: 22rpx;
						color: #2f80ed;
					}
				}

				.distance-box {
					display: flex;
					align-items: center;

					.location {
						width: 20rpx;
						height: 20rpx;
						margin-right: 8rpx;
					}

					.text1 {
						font-weight: 400;
						font-size: 24rpx;
						color: #666666;
					}
				}
			}

			.box2 {
				.text1 {
					font-size: 24rpx;
					color: #999999;
				}

				.text2 {
					width: 1rpx;
					height: 16rpx;
					margin: 0rpx 10rpx;
					color: #979797;
				}
			}
		}

		.box3 {
			align-items: center;
			width: 100%;
			display: flex;

			.text1 {
				font-size: 28rpx;
				color: #666666;
				flex-shrink: 0;
			}

			.con {
				flex: 1;
				display: flex;
				flex-wrap: wrap;
				gap: 20rpx;
				margin-left: 20rpx;

				.empty-text {
					font-size: 28rpx;
					color: #666666;
					flex-shrink: 0;
				}

				.text2 {
					font-size: 24rpx;
					color: #2F80ED;
					font-weight: 500;
					background: #E5F0FF;
					border-radius: 6rpx;
					padding: 10rpx;

				}

				.project {
					color: #2f80ed;
					font-size: 24rpx;
					font-weight: 500;
					background: #e5f0ff;
					padding: 6rpx 10rpx;
					margin-right: 14rpx;
					border-radius: 4rpx;
					white-space: nowrap;
				}
			}
		}

		.footer {
			padding: 10rpx;
			display: flex;
			justify-content: flex-end;
		}
	}

	.colletc {
		width: 25rpx;
		height: 25rpx;
		display: block;
		padding: 10rpx;
		/* 添加点击反馈 */
		touch-action: manipulation;
		&:active {
			opacity: 0.7;
		}
	}
</style>
